<?php

define('APP_OPINIONS', 'opinions');
define('APP_WHITEBOARD', 'whiteboard');

function userToArray($userEntity, $app, $brief = TRUE) {

    if (!$userEntity)
        return NULL;

    $iconUrl = $userEntity->getIcon();
    $userid = $userEntity->getGUID();
    
    // metadata for customize icon
    $metaUserIcon = $userEntity->getMetaData('metaUserIcon');

    if($metaUserIcon!=null) {
            $iconUrl = $metaUserIcon;
    }
    
    $user = array(
        'guid' => $userEntity->getGUID(),
        'username' => $userEntity->username,
        'name' => $userEntity->name,
        'icon' => $iconUrl
        
    );

    if ($brief) {
        
    } else {
        
        $facebooks = get_facebook_accounts_of($userid);
        $facebook = $facebooks[0];
        if($facebook) {
            $user['facebook_id'] = to_facebook_array($facebook);
        }
        /*
        $facebookAccounts = greengar_get_facebook(NULL, $userid);
        if($facebookAccounts && $facebookAccounts[0]){
            $facebookAccount = $facebookAccounts[0];
            $user['facebook_id'] = $facebookAccount->toArray();
        }
        */
        
        $gamecenters = get_gamecenter_accounts_of($userid);
        $gamecenter = $gamecenters[0];
        if($gamecenter) {
            $user['gamecenter_id'] = to_gamecenter_array($gamecenter);
        }
        
        /*
        $gamecenterAccounts = greengar_get_gamecenter(NULL, $userid);
        if($gamecenterAccounts && $gamecenterAccounts[0]){
            $gamecenterAccount = $gamecenterAccounts[0];
            $user['gamecenter_id'] = $gamecenterAccount->toArray();
        }
        */
        
        $loggedin_id = get_loggedin_userid();
        

        if($loggedin_id != $userid) {
            if($app != APP_OPINIONS) {
                $relationship = check_whiteboard_friend($loggedin_id, $userid);
                //$relationship = greengar_check_friend($loggedin_id, $userid, WHITEBOARD);
            } else {
                $relationship = check_opinions_friend($loggedin_id, $userid);
            }
//            $relationship['abc'] = "($userid, $loggedin_id)";
            $user['relationship'] = $relationship;
            
            $relationship = check_whiteboard_follow($loggedin_id, $userid);
//            $relationship['abc'] = "($userid, $loggedin_id)";
            $user['following'] = $relationship;

        }
        
        switch ($app) {
            case APP_OPINIONS:
                $userSocialIds = array();
                /*
                $accounts = greengar_opinions_get_providerAccount(NULL, $userid, 'Gowalla');
                if($accounts && $accounts[0]) {
                    $account = $accounts[0];
                    $userSocialIds[] = $account->toArray();
                }
                $accounts = greengar_opinions_get_providerAccount(NULL, $userid, 'Foursquare');
                if($accounts && $accounts[0]) {
                    $account = $accounts[0];
                    $userSocialIds[] = $account->toArray();
                }*/
                $user['social_ids'] = $userSocialIds;
                
                //$dislike = greengar_opinions_count_interestRating(NULL, $userid, 0);
                //$like = greengar_opinions_count_interestRating(NULL, $userid, 1);
                //$love = greengar_opinions_count_interestRating(NULL, $userid, 2);
                //$neutral = greengar_opinions_count_interestRating(NULL, $userid, -1);
                
                //$lastest_rating_interests = greengar_opinions_get_lastest_interestRating($userid);
                
                $dislike    = count_interest_ratings_of_user($userid, 0);
                $like       = count_interest_ratings_of_user($userid, 2);
                $neutral    = count_interest_ratings_of_user($userid, -1);
                
                $user['like'] = $like;
                $user['dislike'] = $dislike;
                $user['neutral'] = $neutral;
                $user['amount_friends'] = greengar_count_friends($userid, APP_OPINIONS);
                $user['mutual_friends'] = greengar_count_mutual_friends(get_loggedin_userid(), $userid, APP_OPINIONS);
                break;
            case APP_WHITEBOARD:
//                echo 'i am here';
//                echo greengar_whiteboard_count_xmppAccount(NULL, NULL, NULL);
                $accounts = get_whiteboard_xmpp_accounts($userid);
//                echo 1;
                if($accounts && $accounts[0]) {
//                    echo 2;
                    $account = $accounts[0];
//                    echo 3;
                    $user['xmpp_account'] = to_whiteboard_xmpp_account_array($account);
//                    echo 4;
                }
                
                $user['likes_count'] = intval(count_whiteboard_user_likes($userid));
                $user['drawings_count'] = intval(count_drawings_by_owner_id($userid, $userid));
                $user['friends_count'] = intval(count_whiteboard_mutual_follow_of($userid));
                $user['dob'] = NULL;
                $user['gender'] = NULL;
                break;
        };
    }
    return $user;
}

function relationshipToArray($relationshipEntity) {
    if (!$relationshipEntity)
        return array(
            'guid' => -1,
            'relationship' => 'none'
        );

    $guidone = $relationshipEntity->guid_one;
    $guidtwo = $relationshipEntity->guid_two;
    $relationshipjson = $relationshipEntity->relationship;

    if ($relationshipjson) {
        $relationshiparray = json_decode($relationshipjson, true);
        $app = $relationshiparray['app'];
        $relationship = $relationshiparray['relationship'];
    }
    $userone = get_entity($guidone);
    $usertwo = get_entity($guidtwo);

    return array(
        'guid' => $relationshipEntity->id,
        'relationship' => $relationship
    );
}

?>